#!/usr/bin/perl

use Time::localtime;
use File::Copy;
$tm = localtime;

# set some globals
$oldWebstats = "/usr/local/oldWebstats/";
$varLog = "/Inetpub/oldapache2/";
$oldLog = "/Inetpub/oldapache/";

print "You will need to enter the first three letters of the last month:  (ie Oct, Nov, etc) ";
$last_month = <STDIN>;
print "And, the first three letters of the current month: (ie Oct, Nov, etc) ";
$current_month = <STDIN>;

chomp($last_month);
chomp($current_month);
print "$last_month and $current_month\n";

# change the year if last month was december
if ($last_month eq 'Dec')
{
		$current_year = $tm->year+1899;
} 
else 
{
		$current_year = $tm->year+1900;
}

# mkdir /usr/local/oldWebstats/last_month and cp /Inetpub/wwwroot/stats/index.htm to it
# run analog
# (need to set the year to the end of the folder name)
$current_month_year = $last_month . "" . $current_year;
$dir = "" . $oldWebstats . "" . $current_month_year;
# mkdir($dir, 755) or die "Can't create new directory in oldWebstats :( : $!";
print "I just created a new dir " . $dir . "\n";

open LOGFILE_LIST, "VHost.txt";
print "Opening VHost.txt to get list of virtual hosts\n";
while (<LOGFILE_LIST>)
{
    next if /^#/; # skip comments
    # split the array into $access_log, $webroot and $fqdn
    my ($i, $webroot, $fqdn) = split(/:/, $_);
    chomp($webroot);
    chomp($i);
    print "I now have the name of the access_log = " . $i . " and webroot = " . $webroot . " \n";

    # grep out last months info from access_log and place it in /usr/local/oldWebstats/access_logo
    # remove old access logs from /var/log/oldapache
    $remove_old_access_log = "$oldLog$i";
    unlink $remove_old_access_log; 
    print "Removing " . $remove_old_access_log . "\n";

    # grep out the last months entries into /var/log/oldapache
    $last_month_grep_original = "$varLog$i";
    $last_month_grep_new = "$oldLog$i";
    $touch_file = "/bin/touch $last_month_grep_new";
    system($touch_file);
    print "Touching " . $touch_file . "\n";
    
    open( ORIGINAL_FILE, "$last_month_grep_original" ) or die "Can't open last_month_grep_original: $!";
    open( NEW_FILE, "> $last_month_grep_new" ) or die "Can't open last_month_grep_new: $!";
   
    print "I have opened the ORIGINAL_FILE = " . $last_month_grep_original . " and NEW_FILE = " . $last_month_grep_new . " \n";
    print "I am going to now grep out the lines from last month, in ORIGINAL_FILE, into NEW_FILE\n";
    
    while (<ORIGINAL_FILE>)
    {
	$line = $_;
	if ($line =~ m|/$last_month/|) 
	{ 
	    print NEW_FILE "$line"; 
	}
    }
    close(ORIGINAL_FILE);
    close(NEW_FILE);
    print "I am finished greping the files and have closed them...\n";

    # grep this months entries into /var/log/apache/new_$i
    $current_month_grep_original = "$varLog$i";
    $current_month_grep_new = $varLog . "new_" . $i;
    $touch_file2 = "/bin/touch $current_month_grep_new";
    system($touch_file2);
    print "Touching " . $touch_file2 . "\n";
    
    open( ORIGINAL_FILE2, "$current_month_grep_original" ) or die "Can't open current_month_grep_original: $!";
    open( NEW_FILE2, "> $current_month_grep_new" ) or die "Can't open current_month_grep_new: $!";
   
    print "I have opened the ORIGINAL_FILE2 = " . $current_month_grep_original . " and NEW_FILE2 = " . $current_month_grep_new . " \n";
    print "I am going to now grep out the lines from current month, in ORIGINAL_FILE2, into NEW_FILE2\n";
     
    while (<ORIGINAL_FILE2>)
    {
	$line = $_;
	if ($line =~ m|/$current_month/|) 
	{ 
	    print NEW_FILE2 "$line"; 
	}
    }
    close(ORIGINAL_FILE2);
    close(NEW_FILE2);
    print "I am finished grepping the files and have closed them...\n";

    # move new_access_log to access_log
    $mv_access_name = "/bin/mv " . $varLog . "new_" . $i . " $varLog$i\n";
    print "I am moving " . $mv_access_name . "\n";
    system($mv_access_name);

    # mk a new directory to store the old webstats
    $vhost_mkdir = "/bin/mkdir " . $oldWebstats . "" . $current_month_year . "/" . $webroot;
    # system($vhost_mkdir);

    print "Making a new dir = " . $vhost_mkdir . " to store old stats\n";
    $cp_oldstats_location = "/bin/cp /Inetpub/" . $webroot . "/stats/*index.htm $oldWebstats$current_month_year/$webroot";
    print "I am copying " . $old_oldstats_location . " to " . $new_oldstats_location . "\n";
    #copy($old_oldstats_location, $new_oldstats_location) or die "Can't move old stats: $!";
    # system($cp_oldstats_location);
}

# grep out this month info into new_access_log 
# remove error_log and ssl_request_log and finally move
# new_access_log to access_log and restart web server
opendir(DIR, $varLog);
@log_files = readdir(DIR);
print "Now I am going to delete the error_log, ssl_request_log, and php_error\n";
foreach (@log_files)
{
    $file = $_;
    if ( $file =~ m/error_log$/ )
    {
	print "Error log == " . $file;
	$file = $varLog . $file;
	unlink $file;
    }
    elsif ( $file =~ m/ssl_request_log$/ )
    {
	print "SSL log == " . $file;
	$file = $varLog . $file;
	unlink $file;
    }
    elsif ( $file =~ m/php_error$/ )
    {
	print "php_error == " . $file;
	$file = $varLog . $file;
	unlink $file;
    }
}
    
chown www-data,www-data, @chown_log_files;
$restart_apache = "/usr/sbin/apachectl restart";
print "Chowning the new logfiles to www-data\n";
print "Please, restart apache manually, now!!\n";

# print "$restart_apache\n";
# system $restart_apache or die "can't start apache with ssl: $!";

# stats are generated using the GenerateStaticStats function in Stats.py
